package com.foreknow.springboot.controller;

import com.foreknow.springboot.po.Deliveryaddress;
import com.foreknow.springboot.po.User;
import com.foreknow.springboot.service.DeliveryaddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;


@RestController
@RequestMapping("/DeliveryaddressController")
public class DeliveryaddressController {

    @Autowired
    private DeliveryaddressService deliveryaddressService;
    /**
     *根据用户编号查询所属送货地址
     * @param user
     * @return
     */
    @RequestMapping("/listDeliveryAddressByUserId")
    public List<Deliveryaddress> listDeliveryAddressByUserId(Deliveryaddress deliveryaddress){

        return deliveryaddressService.listDeliveryAddressByUserId(deliveryaddress.getUserId());
    }

    /**
     *根据送货地址编号查询送货地址
     * @param deliveryaddress
     * @return
     * @throws SQLException
     */
    @RequestMapping("/getDeliveryAddressById")
    public Deliveryaddress getDeliveryAddressById(Deliveryaddress deliveryaddress){

        return deliveryaddressService.getDeliveryAddressById(deliveryaddress.getDaId());
    }

    /**
     *向送货地址表中添加一条记录
     * @param contactName
     * @param contactSex
     * @param contactTel
     * @param address
     * @param userId
     * @return
     */
    @RequestMapping("/saveDeliveryAddress")
    public int saveDeliveryAddress(Deliveryaddress deliveryaddress){

        return deliveryaddressService.saveDeliveryAddress(deliveryaddress.getContactName(),deliveryaddress.getContactSex(),deliveryaddress.getContactTel(),deliveryaddress.getAddress(),deliveryaddress.getUserId());
    }

    /**
     *根据送货地址编号更新送货地址信息
     * @param daId
     * @param contactName
     * @param contactSex
     * @param contactTel
     * @param address
     * @param userId
     * @return
     */
    @RequestMapping("/updateDeliveryAddress")
    public int updateDeliveryAddress(Deliveryaddress deliveryaddress){

        return deliveryaddressService.updateDeliveryAddress(deliveryaddress.getDaId(),deliveryaddress.getContactName(),deliveryaddress.getContactSex(),deliveryaddress.getContactTel(),deliveryaddress.getAddress(),deliveryaddress.getUserId());
    }

    /**
     *根据送货地址编号删除一条记录
     * @param daId
     * @return
     */
    @RequestMapping("/removeDeliveryAddress")
    public int removeDeliveryAddress(Deliveryaddress deliveryaddress){
        return deliveryaddressService.removeDeliveryAddress(deliveryaddress.getDaId());
    }

}
